<template>
  <div class="login-container">
    <div class="login-box animate-fade-in">
      <div class="login-logo">
        <el-icon class="logo-icon"><Monitor /></el-icon>
      </div>
      <h2 class="login-title">MyAI 智能问答系统</h2>
      <p class="login-subtitle">基于大语言模型的智能助手</p>
      <el-form :model="loginForm" ref="loginFormRef" :rules="rules" class="login-form" @submit.prevent="handleLogin(loginFormRef)">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名" 
            :prefix-icon="User"
            class="custom-input" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            :prefix-icon="Lock"
            show-password
            class="custom-input" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="handleLogin(loginFormRef)" :loading="loading">
            <span>登录系统</span>
            <el-icon class="el-icon--right"><ArrowRight /></el-icon>
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-options">
        <span>没有账户？</span>
        <el-button type="text" @click="goToRegister">立即注册</el-button>
      </div>
      <div class="login-footer">
        <p>© MyAI 智能问答系统</p>
      </div>
    </div>
    <div class="login-decoration">
      <div class="floating-circle circle-1"></div>
      <div class="floating-circle circle-2"></div>
      <div class="floating-circle circle-3"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Monitor, ArrowRight } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

// 响应式数据
const loginForm = reactive({
  username: '',
  password: ''
})

const loading = ref(false)

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)

/**
 * 处理登录
 * @param {object} formEl - 表单元素引用
 */
const handleLogin = async (formEl) => {
  if (!formEl) return
  
  await formEl.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        
        // 使用认证store进行登录
        await authStore.login(loginForm)
        
        // 登录成功后跳转
        const redirectPath = route.query.redirect || '/main/chat'
        router.push(redirectPath)
        
      } catch (error) {
        console.error('登录失败:', error)
        ElMessage.error(error.message || '登录失败，请检查用户名和密码')
      } finally {
        loading.value = false
      }
    } else {
      ElMessage.error('请填写完整的登录信息')
    }
  })
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/images/login-bg.svg');
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
}

.login-box {
  width: 440px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  z-index: 10;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.login-logo {
  text-align: center;
  margin-bottom: 20px;
}

.logo-icon {
  font-size: 48px;
  color: var(--primary-color);
  background-color: var(--primary-light);
  padding: 16px;
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

.login-title {
  text-align: center;
  margin-bottom: 10px;
  color: var(--text-primary);
  font-size: 28px;
  font-weight: 600;
}

.login-subtitle {
  text-align: center;
  margin-bottom: 30px;
  color: var(--text-secondary);
  font-size: 16px;
}

.login-form {
  margin-bottom: 20px;
}

.custom-input :deep(.el-input__wrapper) {
  padding: 12px;
  box-shadow: 0 0 0 1px var(--border-light) inset;
  transition: all 0.3s;
}

.custom-input :deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px var(--primary-color) inset;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px var(--primary-color) inset !important;
}

.login-btn {
  width: 100%;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  transition: all 0.3s;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.login-options {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20px;
  color: #606266;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  color: var(--text-secondary);
  font-size: 14px;
}

/* 装饰元素 */
.login-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.floating-circle {
  position: absolute;
  border-radius: 50%;
  opacity: 0.6;
}

.circle-1 {
  width: 200px;
  height: 200px;
  background: linear-gradient(45deg, #4b6cb7, #182848);
  top: 10%;
  left: 15%;
  animation: float 8s ease-in-out infinite;
}

.circle-2 {
  width: 120px;
  height: 120px;
  background: linear-gradient(45deg, #4b6cb7, #182848);
  bottom: 20%;
  right: 15%;
  animation: float 6s ease-in-out infinite;
  animation-delay: 1s;
}

.circle-3 {
  width: 80px;
  height: 80px;
  background: linear-gradient(45deg, #4b6cb7, #182848);
  top: 30%;
  right: 25%;
  animation: float 10s ease-in-out infinite;
  animation-delay: 2s;
}

@keyframes float {
  0% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(5deg);
  }
  100% {
    transform: translateY(0) rotate(0deg);
  }
}
</style>